package com.android.svmc.app.mobile.controller;

import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;

import com.android.svmc.app.mobile.data.DatabaseSource;
import com.android.svmc.app.mobile.data.JsonData;
import com.android.svmc.app.mobile.model.PostDetailOffline;
import com.android.svmc.app.mobile.util.CommonDefine;
import com.android.svmc.app.mobile.util.SettingUtils;

public class SaveDataService extends Service{
	private ScheduledExecutorService scheduleTaskExecutor;
	final int FIRST_PAGE = 1;
	loadPost loadPost ;
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		SettingUtils.savebooleanPreferences(this, CommonDefine.IS_STARTED_SERVICE_OFFLINE_MODE, true);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if( loadPost != null && !loadPost.isCancelled()){
			loadPost.cancel(true);
		}
		if (scheduleTaskExecutor != null && !scheduleTaskExecutor.isShutdown()){
			scheduleTaskExecutor.shutdown();
		}
		SettingUtils.savebooleanPreferences(this, CommonDefine.IS_STARTED_SERVICE_OFFLINE_MODE, false);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
		scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				if (WifiManagement.haveNetworkConnection() == true){
					loadPost = new loadPost();
					loadPost.execute();
					Log.i(CommonDefine.TAG, "onStartCommand service savedata");
				}
				
			}
		}, 0, 3, TimeUnit.DAYS);
		
		return START_STICKY;
	}
	public class loadPost extends AsyncTask<Void, Void, Void>{
		DatabaseSource data = new DatabaseSource(SaveDataService.this);
		SDCardController sdcard = new SDCardController(SaveDataService.this);
		String listpicture;
		ArrayList<PostDetailOffline> listpost = new ArrayList<PostDetailOffline>();
		ArrayList<PostDetailOffline> listtruyencuoi = new ArrayList<PostDetailOffline>();
		ArrayList<PostDetailOffline> listtruyenngan = new ArrayList<PostDetailOffline>();
		ArrayList<PostDetailOffline> listhatgiongtamhon = new ArrayList<PostDetailOffline>();
		ArrayList<PostDetailOffline> listtruyennganvoz = new ArrayList<PostDetailOffline>();
		ArrayList<PostDetailOffline> listtamsubandoc = new ArrayList<PostDetailOffline>();
		@Override
		protected Void doInBackground(Void... params) {
			JsonData json = new JsonData(SaveDataService.this);
			listtruyenngan = json.getPostOffline(CommonDefine.TRUYENNGAN, FIRST_PAGE);
			listhatgiongtamhon=json.getPostOffline(CommonDefine.HATGIONGTAMHON, FIRST_PAGE);
			listtruyennganvoz=json.getPostOffline(CommonDefine.TRUYENNGANVOZ, FIRST_PAGE);
			listtamsubandoc = json.getPostOffline(CommonDefine.TAMSUBANDOC, FIRST_PAGE);
			listtruyencuoi = json.getPostOffline(CommonDefine.TRUYENCUOI, FIRST_PAGE);
			
			listpost.addAll(listtruyencuoi);
			listpost.addAll(listtruyenngan);
			listpost.addAll(listhatgiongtamhon);
			listpost.addAll(listtamsubandoc);
			listpost.addAll(listtruyennganvoz);
			data.clearDatabase();
			for(PostDetailOffline post : listpost){
				data.addPost(post);
			}
			
			listpicture = json.getListPictureOffline(CommonDefine.DEFAULT_ID, 1);
			sdcard.writeOneLineToSdcard(listpicture, CommonDefine.APP_FOLDER + "/list_picture.txt");
			return null;
		}
	
		
		
		
	}
	
}
